package web;
import driverSelf.*;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.Select;

public class WebKeyword {
    //所有对浏览器的基本操作都要用的，浏览器对象
    private WebDriver driver;
    public WebDriver getDriver(){
        return driver;
    }
    public void setDriver(WebDriver driver){
        this.driver=driver;
    }
    /*
    * 启动浏览器，将webdriver对象赋值
    * */
    public void openBrowser(String browserType){
        switch(browserType){
            case "火狐":
                FireFoxDr ff=new FireFoxDr("","webdriverExe\\geckodriver.exe");
                driver=ff.getDriver();
                break;
            case "ie":
                IEDriver ieDriver=new IEDriver("webdriverExe\\IEDriverServer.exe");
                driver=ieDriver.getDriver();
                break;
            case "edge":
                System.setProperty("webdriver.edge.driver","webdriverExe\\msedgedriver.exe");
                break;
            case "chrome":
            default:
                ChromeDr chr=new ChromeDr("chromedriver.exe");
                driver=chr.getDriver();
        }
    }
    public void visitWeb(String url){
        try{
            driver.get(url);
        }catch (Exception e){
            System.out.println(e.fillInStackTrace());
            System.out.println("输入的url地址有问题，无法访问网页");
        }
    }
    public void visitPage(String url) {
        try {
            driver.get(url);
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
            System.out.println("url请检查是否正确");
        }
    }

    public void setWindowSize(int leftCorner,int height,int width){
        driver.manage().window().setPosition(new Point(leftCorner,0));
        driver.manage().window().setSize(new Dimension(height,width));
    }
    public void inputByCss(String css,String content){
        try{
            WebElement element=driver.findElement(By.cssSelector(css));
            element.clear();
            element.sendKeys(content);
        }catch(Exception e){
            System.out.println(e.fillInStackTrace());
            System.out.println("向元素"+css+"输入内容"+content+"失败");
        }
    }
    public void selectByValue(String xpath,String value){
        try {
            WebElement element = driver.findElement(By.xpath(xpath));
            Select select=new Select(element);
            select.selectByValue(value);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("操作的select元素定位失败或没有找到对应的value"+value);
        }
    }

    public void clickById(String id){
        try{
            driver.findElement(By.id(id)).click();
        }catch (Exception e){
            System.out.println(e.fillInStackTrace());
        }
    }
    public void switchFrame(String locator){
        try {
            // 斜杠开头就是xpath，定位在切换
            if(locator.startsWith("/")){
                WebElement frame = driver.findElement(By.xpath(locator));
                driver.switchTo().frame(frame);
            }else{
                //通过name or id形式切换。
                driver.switchTo().frame(locator);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void selectByText(String xpath,String text){
        try {
            Select select=new Select(driver.findElement(By.xpath(xpath)));
            select.selectByVisibleText(text);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("操作的select元素定位失败或没有找到对应的文本"+text);
        }
    }
    public void clickButton(String xpath1) {
        try {
            driver.findElement(By.xpath(xpath1)).click();
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
        }
    }
    public void input(String xpath,String content){
        try{
            WebElement element=driver.findElement(By.xpath(xpath));
            element.clear();
            element.sendKeys(content);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void inputAll(String method, String locator,String content){
        switch (method){
            case "id":
                driver.findElement(By.id(locator)).sendKeys(content);
                break;
            case "css":
            case "xpath":
        }
    }
    public void halt(String second){
        try{
            float times=Float.parseFloat(second);
            Thread.sleep((long)(times*1000));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void assertEleAttr(String xpath,String attr,String expectResult){
        try{
            WebElement element=driver.findElement(By.xpath(xpath));
            String attribute=element.getAttribute(attr);
            if(attribute.equals(expectResult)){
                System.out.println("测试成功");
            }else{
                System.out.println("测试失败");
                System.out.println("预期结果是"+expectResult);
            }

        }catch (Exception e){
            System.out.println("测试失败，获取元素的属性出错了");
        }
    }
    public void upload(String xpath,String filePath){
        input(xpath,filePath);
    }
    public void runJs(String script){
        try{
            JavascriptExecutor jsRunner=(JavascriptExecutor)driver;
            jsRunner.executeScript(script);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("执行js"+script+"失败");
        }

    }
    public void runJSWithElement(String xpah,String script){
        try{
            WebElement element=driver.findElement(By.xpath(xpah));
            JavascriptExecutor jsRunner=(JavascriptExecutor)driver;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("执行js语句"+"argument[0]"+script+"失败");
        }
    }
    public void switchParent(){
        try{
            driver.switchTo().parentFrame();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("切换到父级iframe失败");
        }
    }
    public void closeBrowser(){
        driver.quit();
    }
}
